/**
 * 设备管理初始化
 */
var RealTimeLog = {
    logTableId: "LogTable",
    deviceTableId: "DeviceTable",
    seItem: null,
    deviceTable: null,
    logTable: null,
    layerIndex: -1,
    interval:null
};
function listEquipment () {
    $.post(Feng.ctxPath + '/door/real/time/listEquipment', {order: 'desc', offset: 0, limit: 14},
        function(data) {
            var html = '';
            for (var i = 0; i < data.length; i++) {
                html +='<label>'+
                    '<input type="radio" name="doorNum" value="'+ data[i].id +'" style="display:none;" />'+
                    '<div data-v-52a8b34e="" class="ivu-poptip">' +
                    '    <div class="ivu-poptip-rel">' +
                    '        <img id="'+ data[i].id +'" data-v-52a8b34e="" src='+ (data[i].openStatus == 1 ? "../../../../../static/img/lianjie.png" : "../../../../../static/img/duankai.png") +' alt="">' +
                    '    </div>' +
                    '    <div class="ivu-poptip-popper" style="position: absolute; will-change: top, left; display: none;" x-placement="bottom-start">' +
                    '        <div class="ivu-poptip-content">' +
                    '            <div class="ivu-poptip-arrow"></div>' +

                    '            <div class="ivu-poptip-inner">' +
                    '                <div class="ivu-poptip-title">' +
                    '                    <div class="ivu-poptip-title-inner">状态</div>' +
                    '                </div>' +
                    '                <div class="ivu-poptip-body">' +
                    '                    <div class="ivu-poptip-body-content">' +
                    '                        <div data-v-52a8b34e="">' +
                    '                            <ul data-v-52a8b34e="">' +
                    '                                <li data-v-52a8b34e="">设备名称: '+ data[i].equipmentName +'</li>' +
                    '                                <li data-v-52a8b34e="">通讯方式: ' + data[i].communicationName + '</li>' +
                    '                                <li data-v-52a8b34e="">IP地址: ' + data[i].ipAddress + '</li>' +
                    '                                <li data-v-52a8b34e="">门禁控制器类型: ' + data[i].controllerName + '</li>' +
                    '                                <li data-v-52a8b34e="">区域: ' + data[i].areaName + '</li>' +
                    '                                <li data-v-52a8b34e="">序列号: ' + data[i].serialNumber + '</li>' +
                    '                            </ul>' +
                    '                        </div>' +
                    '                    </div>' +
                    '                </div>' +
                    '            </div>' +
                    '        </div>' +
                    '</div>' +
                    '</div>' +
                    '</label>'
            }
            $('.a').html(html);
            $('.ivu-poptip-rel').mouseover(function () {
                $(this).next().css({"display":"block"})
            });
            $('.ivu-poptip-rel').mouseout(function () {
                $(this).next().css({"display":"none"})
            });
            $("input[name='doorNum']").click(function () {
                $("img").each(function(){
                    if($(this).attr("id") == $("input[name='doorNum']:checked").val()) {
                        $(this).css({'border': '2px solid #000'})
                    } else {
                        $(this).css({'border': '1px solid rgb(226, 226, 226)'})
                    }
                });
                console.log($("input[name='doorNum']:checked").val())
            })
        })
}
/**
 * 初始化表格的列
 */
RealTimeLog.initDeviceColumn = function () {
    return [
        {field: 'selectItem', radio: true},
        {title: 'ID', field: 'id', visible: true, align: 'center', valign: 'middle'},
        {title: '设备名称', field: 'equipmentName', visible: true, align: 'center', valign: 'middle'},
        {title: '通信方式', field: 'communicationName', visible: true, align: 'center', valign: 'middle'},
        {title: 'IP地址', field: 'ipAddress', visible: true, align: 'center', valign: 'middle'},
        {title: '通信端口', field: 'communicationPost', visible: true, align: 'center', valign: 'middle'},
        {title: '通信密码', field: 'communicationPassword', visible: true, align: 'center', valign: 'middle'},
        {title: '图标类型', field: 'iconType', visible: true, align: 'center', valign: 'middle'},
        {title: '门禁控制器类型', field: 'controllerName', visible: true, align: 'center', valign: 'middle'},
        {title: '区域', field: 'areaName', visible: true, align: 'center', valign: 'middle'},
        {title: '序列号', field: 'serialNumber', visible: true, align: 'center', valign: 'middle'},
        {title: '开启状态', field: 'openStatus', visible: true, align: 'center', valign: 'middle'},
        {title: '在线状态', field: 'onlineStatus', visible: true, align: 'center', valign: 'middle'},
    ];
};

/**
 * 初始化表格的列
 */
RealTimeLog.initLogColumn = function () {
    return [
        {field: 'selectItem', radio: true},
        {title: 'ID', field: 'id', visible: true, align: 'center', valign: 'middle'},
        {title: '时间', field: 'timeSecond', visible: true, align: 'center', valign: 'middle'},
        {title: '设备名称', field: 'equipmentName', visible: true, align: 'center', valign: 'middle'},
        {title: '门ID', field: 'doorName', visible: true, align: 'center', valign: 'middle'},
        {title: '事件名称', field: 'eventName', visible: true, align: 'center', valign: 'middle'},
        {title: '人员编号', field: 'pin', visible: true, align: 'center', valign: 'middle'},
        {title: '姓名', field: 'userName', visible: true, align: 'center', valign: 'middle'},
        {title: '卡号', field: 'cardNo', visible: true, align: 'center', valign: 'middle'},
        {title: '验证方式', field: 'verifiedName', visible: true, align: 'center', valign: 'middle'},
        {title: '进出状态', field: 'inoutState', visible: true, align: 'center', valign: 'middle'},
        {title: '区域名称', field: 'areaName', visible: true, align: 'center', valign: 'middle'},
        {title: '是否正常', field: 'eventStatus', visible: true,
            cellStyle: function (value, row, index) {
                if (value == 0) {
                    return {css:{"background-color":"red"}};
                }else if(value == 1) {
                    return {css:{"background-color":"green"}};
                }
            },
            formatter: function (value, row, index) {
                return '';
            },
            align: 'center', valign: 'middle'},
    ];
};
/**
 * 检查是否选中
 */
RealTimeLog.check = function () {
    var selected = $('#' + this.deviceTableId).bootstrapTable('getSelections');
    if($("input[name='doorNum']:checked").val() == ""){
        Feng.info("请先选中表格中的某一记录！");
        return false;
    }else{
        RealTimeLog.seItem = $("input[name='doorNum']:checked").val();
        return true;
    }
};

/**
 * 开启实时监控
 */
RealTimeLog.openRealTime = function () {
    if (this.check()) {
        var ajax = new $ax(Feng.ctxPath + "/door/real/time/openRealTime", function (data) {
            Feng.success(data.message);
            RealTimeLog.deviceTable.refresh();
        }, function (data) {
            Feng.error("开启失败!" + data.responseJSON.message + "!");
        });
        ajax.set("id",this.seItem);
        ajax.start();
        listEquipment();
    }
};

/**
 * 关闭实时监控
 */
RealTimeLog.closeRealTime = function () {
    if (this.check()) {
        var ajax = new $ax(Feng.ctxPath + "/door/real/time/closeRealTime", function (data) {
            Feng.success(data.message);
            RealTimeLog.deviceTable.refresh();
        }, function (data) {
            Feng.error("关闭失败!" + data.responseJSON.message + "!");
        });
        ajax.set("id",this.seItem);
        ajax.start();
        listEquipment();
    }
};

/**
 * 获取实时数据
 */
RealTimeLog.getRealTimeLog = function () {

    // RealTimeLog.logTable.refresh();
    var ajax = new $ax(Feng.ctxPath + "/door/real/time/realTimeStart", function (data) {
        Feng.success(data.message);
        // RealTimeLog.logTable.refresh();
    }, function (data) {
        Feng.error("关闭失败!" + data.responseJSON.message + "!");
    });
    ajax.start();
    RealTimeLog.interval = setInterval("RealTimeLog.getRealTimeLogRecrod()",3000);
};
RealTimeLog.getRealTimeLogRecrod = function () {

    var ajax = new $ax(Feng.ctxPath + "/door/real/time/listRealTimeRecord",
        function (data) {
            $("#LogTable").bootstrapTable('prepend',data);
        }, function (data) {
            Feng.error("关闭失败!" + data.responseJSON.message + "!");
        });
    ajax.set("id",this.seItem.id);
    ajax.start();
};
/**
 * 关闭获取数据
 */
RealTimeLog.closeRealTimeLogRecord = function () {
    var ajax = new $ax(Feng.ctxPath + "/door/real/time/realTimeStop", function (data) {
        Feng.success(data.message);
        RealTimeLog.deviceTable.refresh();
    }, function (data) {
        Feng.error("关闭获取数据失败!" + data.responseJSON.message + "!");
    });
    ajax.start();
    clearInterval(RealTimeLog.interval );//停止
    listEquipment();

};
/**
 * 查询门信息列表
 */
RealTimeLog.search = function () {
    var queryData = {};
    queryData['equipmentName'] = $("#equipmentName").val();
    RealTimeLog.deviceTable.refresh({query: queryData});
};
/**
 * 重置查询条件
 */
RealTimeLog.reset = function () {
    $('#equipmentName').val('');
    this.search();
};
$(function () {
    //设备列表
    var deviceColunms = RealTimeLog.initDeviceColumn();
    var deviceTable = new BSTable(RealTimeLog.deviceTableId, "/door/real/time/listEquipment", deviceColunms);
    deviceTable.setPaginationType("client");
    RealTimeLog.deviceTable = deviceTable.init();
    listEquipment();

    //日志列表
    var initLogColumn = RealTimeLog.initLogColumn();
    var logTable = new BSTable(RealTimeLog.logTableId, "/door/real/time/listRealTimeRecord", initLogColumn);
    logTable.setPaginationType("client");
    RealTimeLog.logTable = logTable.init();
});
/**
 * 获取实时数据
 */
RealTimeLog.getRealTimeLog1 = function () {

    // RealTimeLog.logTable.refresh();
    var ajax = new $ax(Feng.ctxPath + "/door/real/time/realTimeStart", function (data) {
        Feng.success(data.message);
        // RealTimeLog.logTable.refresh();
    }, function (data) {
        Feng.error("关闭失败!" + data.responseJSON.message + "!");
    });
    ajax.start();

    setInterval("RealTimeLog.logTable.refresh()",3000);
    // if (this.check()) {
    //     var ajax = new $ax(Feng.ctxPath + "/door/real/time/listRealTimeRecord", function (data) {
    //         Feng.success(data.message);
    //         RealTimeLog.logTable.refresh();
    //     }, function (data) {
    //         Feng.error("关闭失败!" + data.responseJSON.message + "!");
    //     });
    //     ajax.set("id",this.seItem.id);
    //     ajax.start();
    // }
};